<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="{ '--width': pWidth + 'rpx', '--height': pHeight + 'rpx', left: left + 'px', top: top + 'px' }"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend="touchend"
			@click.stop.prevent="click"
			:class="{transition: isDock && !isMove }"
		>
      <image class="img" :src="imageUrl" v-if="imageUrl"></image>
			<text v-else>{{ text }}</text>

      <view class="count-tips" v-if="countTipsNum > 0">
        {{ countTipsNum }}
      </view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
      // 是否启用自动停靠
			isDock:{
				type: Boolean,
				default: false
			},
      // 当前页面是否包含tabbar
			existTabBar:{
				type: Boolean,
				default: false
			},
      // 按钮宽度
      pWidth: {
        type: Number,
        default: 100
      },
      // 按钮高度
      pHeight: {
        type: Number,
        default: 100
      },
      // 图片地址
      imageUrl: {
        type: String,
        default: ''
      },
      // 按钮文字
      text: {
        type: String,
        default: '按钮'
      },
      // 距离底部高度
      bottomH: {
        type: Number,
        default: 50
      },
      // 统计数量
      countTipsNum: {
        type: Number,
        default: 0
      }
		},
		data() {
			return {
        // 按钮位置 top
				top: 0,
        // 按钮位置 left
				left: 0,
        // 按钮宽度
				width: 0,
        // 按钮高度
				height: 0,
        // 按钮宽度一半
				offsetWidth: 0,
        // 按钮高度一半
				offsetHeight: 0,
        // 屏幕宽度
				windowWidth: 0,
        // 屏幕高度
				windowHeight: 0,
        // 是否移动中
				isMove: true,
        // 边界值
				edge: 10,
			}
		},
		mounted() {
      // 获取系统信息
			const sys = uni.getSystemInfoSync();

			// 屏幕宽度、高度
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			
			// #ifdef APP-PLUS
				this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log('sys', sys);
			
      // 获取按钮节点信息
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = this.windowHeight - this.height - this.edge - this.bottomH;
			}).exec();
		},
		methods: {
      // 点击事件
			click() {
				this.$emit('btnClick');
			},
      // 触摸开始
			touchstart(e) {
				this.$emit('btnTouchStart');
			},
      // 触摸移动中
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				
				this.isMove = true;
				
        // x坐标
				let clientX = e.touches[0].clientX - this.offsetWidth;
        // y坐标
				let clientY = e.touches[0].clientY - this.offsetHeight;

				// #ifdef H5
					clientY += this.height;
				// #endif

        // 右边界值
        let edgeRight = this.windowWidth - this.width - this.edge;
        // 下边界值
				let edgeBottom = this.windowHeight - this.height - this.edge;


        // 左右触及边界
        if (clientX < this.edge) {
					this.left = this.edge;
				} else if (clientX > edgeRight) {
					this.left = edgeRight;
				} else {
					this.left = clientX
				}

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				
			},
      // 触摸结束
			touchend(e) {
				if (this.isDock) {
          // 右边界值
					let edgeRight = this.windowWidth - this.width - this.edge;
					
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRight;
					}
					
				}
				
				this.isMove = false;
				
				this.$emit('btnTouchEnd');
			},
		}}
</script>

<style lang="scss">
	.drag {
		position: fixed;
		z-index: 99;
    width: var(--width);
    height: var(--height);
		font-size: $uni-font-size-sm;
		color: #ffffff;
		border-radius: 50%;
		background-color: #5769AF;
		box-shadow: 0 0 6rpx rgba(0, 0, 0, 0.4);
		display: flex;
		justify-content: center;
		align-items: center;
		
		&.transition {
			transition: all .3s ease;
		}

    .count-tips {
      position: absolute;
      top: 0;
      right: 0;
      padding: 6rpx 10rpx;
      font-size: 22rpx;
      line-height: 22rpx;
      font-weight: bold;
      color: #FFFFFF;
      background: #FF7204;
      border-radius: 30rpx;
    }
	}

  .img {
    width: var(--width);
    height: var(--height);
    display: block;
  }
	
</style>
